package com.zhangruile.dao;

import com.zhangruile.model.TrainModel;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

/**
 * ClassName: TrainDao
 *
 * @author :张蕊乐
 *  version:1.0.0
 * @since JDK 1.8
 * date: 2017 11 09 19:54
 */
public class TrainDao extends BaseDao {
    public Map<String,Object> getPrice(String trainNum, String startStation, String endStation) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(getDataSource());
        String sql="SELECT s1.trainNum,s1.station AS startStation,s2.station AS endStation,s2.sitePrice -s1.sitePrice AS price FROM station s1,station s2 WHERE s1.trainNum=s2.trainNum AND s1.trainNum=? AND s1.station=? AND " +
                "s2.station=?";

        Map<String,Object> map=queryRunner.query(sql, new ResultSetHandler<Map<String,Object>>() {
            @Override
            public Map<String,Object> handle(ResultSet resultSet) throws SQLException {
                if (resultSet.next()){
                    Map<String,Object> result=new HashedMap();
                    result.put("trainNum",resultSet.getString("trainNum"));
                    result.put("startStation",resultSet.getString("startStation"));
                    result.put("endStation",resultSet.getString("endStation"));
                    result.put("price",resultSet.getFloat("price"));
                    return result;
                }
                return null;
            }
        }, trainNum, startStation, endStation);

        return  map;
    }

}
